package com.gshrms.framework.web.service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.gshrms.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import com.gshrms.common.core.domain.entity.SysRole;
import com.gshrms.common.core.domain.entity.SysUser;
import com.gshrms.system.service.ISysMenuService;
import com.gshrms.system.service.ISysRoleService;

/**
 * 用户权限处理
 *
 * @author ruoyi
 */
@Component
public class SysPermissionService {
    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysUserMapper userMapper;

    /**
     * 获取角色数据权限
     *
     * @param user 用户信息
     * @return 角色权限信息
     */
    public Set<String> getRolePermission(SysUser user) {
        Set<String> roles = new HashSet<String>();
        // 管理员拥有所有权限

        roles.addAll(roleService.selectRolePermissionByUserId(user.getUserId()));

        return roles;
    }

    /**
     * 获取菜单数据权限
     *
     * @param user 用户信息
     * @return 菜单权限信息
     */
    public Set<String> getMenuPermission(SysUser user) {
        SysUser sysUser = userMapper.selectUserById(user.getUserId());
        Set<String> perms = new HashSet<String>();
        //超级管理员返回所有权限
        if (sysUser.getUserType().equals("01")) {
            //如果是主管理员登录则返回isSystem为0的按钮权限
            Set<String> rolePerms = menuService.getMasterAdminMenusButton();
            perms.addAll(rolePerms);
        } else if (sysUser.getUserType().equals("02")) {
            //如果是子管理员,查询有哪些权限
            Set<String> rolePerms = menuService.getSubAdminMenusButtonByUserId(user.getUserId());
            perms.addAll(rolePerms);
        } else {
            //不是管理员返回空
        }


//        // 管理员拥有所有权限
//        if (user.isAdmin())
//        {
//            perms.add("*:*:*");
//        }
//        else
//        {
//            List<SysRole> roles = user.getRoles();
//            if (!CollectionUtils.isEmpty(roles))
//            {
//                // 多角色设置permissions属性，以便数据权限匹配权限
//                for (SysRole role : roles)
//                {
//                    Set<String> rolePerms = menuService.selectMenuPermsByRoleId(role.getRoleId());
//                    role.setPermissions(rolePerms);
//                    perms.addAll(rolePerms);
//                }
//            }
//            else
//            {
//                perms.addAll(menuService.selectMenuPermsByUserId(user.getUserId()));
//            }
//        }
        return perms;
    }

    //查询员工端页面权限
    public Set<String> getMenuPermissionNoAdmin(SysUser user) {
        Set<String> rolePerms = menuService.getStaffMenusButtonByUserId(user.getUserId());
        return rolePerms;
    }
}
